package com.example.chat.service.impl;

import com.dragonsoftbravo.libs.PssEncryptionUtils;
import com.example.chat.constant.ErrorCode;
import com.example.chat.constant.ErrorMessageConstant;
import com.example.chat.dao.PermissionDao;
import com.example.chat.dao.RolePermissionDao;
import com.example.chat.model.*;
import com.example.chat.model.dto.PermissionDTO;
import com.example.chat.model.ro.LoginRO;
import com.example.chat.model.vo.*;
import com.example.chat.repository.CompanyRepository;
import com.example.chat.repository.EmployeeRepository;
import com.example.chat.repository.RegionRepository;
import com.example.chat.service.CodeMessageService;
import com.example.chat.service.JwtService;
import com.example.chat.service.LoginService;
import com.example.chat.utils.ChatException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @Description
 * @Date 2022/11/16 22:47
 * @Author pq
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Value("${back.password.salt}")
    private String passwordSalt;

    @Resource
    private EmployeeRepository employeeRepository;

    @Resource
    private RolePermissionDao rolePermissionDao;

    @Resource
    private JwtService jwtService;

    @Resource
    private RegionRepository regionRepository;

    @Resource
    private CompanyRepository companyRepository;

    @Override
    public LoginVO login(LoginRO ro) {
        Employee employee = employeeRepository.findByNameAndDeletedIsFalse(ro.getUsername()).orElseThrow(() -> new ChatException(ErrorMessageConstant.ACCOUNT_NOT_EXIST));
        if (!employee.getActive()) {
            throw new ChatException(ErrorMessageConstant.ACCOUNT_NOT_ACTIVE);
        }
        String pwd = PssEncryptionUtils.encryptMD5(passwordSalt, ro.getPassword());
        if (StringUtils.equals(employee.getPassword(), pwd)) {
            throw new ChatException(ErrorMessageConstant.PASSWORD_ERROR);
        }
        TokenInfoVo tokenInfoVo = new TokenInfoVo();
        Set<String> permissionCodes = (rolePermissionDao.getRolePermission(employee.getId())).stream().map(PermissionDTO::getPermissionCode).collect(Collectors.toSet());
        Set<String> roleNames = rolePermissionDao.getRolePermission(employee.getId()).stream().map(PermissionDTO::getRoleName).collect(Collectors.toSet());
        tokenInfoVo.setPermissionIds(permissionCodes);
        tokenInfoVo.setEmpId(employee.getId());
        String loginUUID = UUID.randomUUID().toString();
        tokenInfoVo.setLoginUUID(loginUUID);
        String token = jwtService.getToken(tokenInfoVo);
        LoginVO loginVO = new LoginVO();
        loginVO.setEmpId(employee.getId());
        loginVO.setEmpName(employee.getName());
        DropDownVo regionVO = new DropDownVo();
//        Region region = regionRepository.findByIdAndDeletedIsFalse(employee.getRegionId()).orElseThrow();
//        if (Objects.nonNull(region)) {
//            regionVO.setId(region.getId());
//            regionVO.setName(region.getCity());
//        }
        loginVO.setRegionVO(regionVO);
        loginVO.setToken(token);
        loginVO.setPermission(permissionCodes);
        loginVO.setDeptId(employee.getDeptId());
        loginVO.setRoleNames(roleNames);
//        Company company = companyRepository.findByIdAndDeletedIsFalse(employee.getCompanyId()).orElseGet(null);
//        CompanyVO companyVO = CompanyVO.instance(company);
//        loginVO.setCompanyVO(companyVO);
        return loginVO;
    }
}
